with Ada.Text_IO;
with PolyORB.Parameters;

package body Server is

	-- inizializzazione dei termini per il client
	procedure GetInit (Field : in out Field_Access; Ball : in out Ball_Access; Time : in out TimeCount_Access; Timer : in out Timer_Access) is
	begin
		Field := F;
		Ball := B;
		Time := Tm;
		Timer := Tmr;
        Ada.Text_IO.Put_Line("+++ - Client Connected - +++");
	end GetInit;

    function GetField return Field_Access is
    begin
        return F;
    end GetField;
    
    function GetBall return Ball_Access is
    begin
        return B;
    end GetBall;

    function GetTime return TimeCount_Access is
    begin
        return Tm;
    end GetTime;
    
    function GetTimer return Timer_Access is
    begin
        return Tmr;
    end GetTimer;

	procedure Start is

		-- la procedura server inizializza il task SERVER che accetta i client
		-- e far partire l'inizializzazione delle componenti

		-- il server inizializza le sue componenti (Field, Ball, Timer, TaskBall, Referee)
		-- e 2 client connessi (Player, Manager, Team, GUI con Gtk)
	begin

        -- messaggio Start server
        Ada.Text_IO.Put_Line("*** - Server Init - ***");

		-- inizializzazione delle posizioni
		F.InitField;

    	-- inizializzazione dei giocatori
    	-- ID, Role, Team, Direction, Field, Ball, Visibility, Speed, Power, Precision
    	Players( 1) := new Player ( 1, 	GKP, 	T1, 	1,	    F, B, 60, 60, 60, 70);
    	Players( 2) := new Player ( 2, 	LDF, 	T1, 	1,		F, B, 60, 50, 50, 70);
    	Players( 3) := new Player ( 3, 	CDF, 	T1, 	1,		F, B, 60, 60, 60, 70);
    	Players( 4) := new Player ( 4, 	CDF, 	T1, 	1,		F, B, 60, 70, 70, 70);
    	Players( 5) := new Player ( 5, 	RDF, 	T1, 	1,		F, B, 60, 60, 60, 70);
    	Players( 6) := new Player ( 6, 	LMF, 	T1, 	1,		F, B, 60, 70, 70, 70);
    	Players( 7) := new Player ( 7, 	CMF, 	T1, 	1,		F, B, 60, 40, 90, 70);
    	Players( 8) := new Player ( 8, 	CMF, 	T1, 	1,		F, B, 60, 60, 70, 70);
    	Players( 9) := new Player ( 9, 	RMF, 	T1, 	1,		F, B, 60, 50, 60, 70);
    	Players(10) := new Player (10, 	LFW, 	T1, 	1,		F, B, 60, 40, 80, 70);
    	Players(11) := new Player (11, 	RFW, 	T1, 	1,		F, B, 60, 70, 60, 70);
    	Players(12) := new Player (12, 	GKP, 	T2, 	-1, 	F, B, 60, 70, 70, 70);
    	Players(13) := new Player (13, 	LDF,	T2, 	-1, 	F, B, 60, 80, 80, 70);
    	Players(14) := new Player (14, 	CDF,	T2, 	-1, 	F, B, 60, 70, 70, 70);
    	Players(15) := new Player (15, 	CDF,	T2, 	-1, 	F, B, 60, 60, 60, 70);
    	Players(16) := new Player (16, 	RDF,	T2, 	-1, 	F, B, 60, 50, 50, 70);
    	Players(17) := new Player (17, 	LMF, 	T2, 	-1, 	F, B, 60, 60, 60, 70);
    	Players(18) := new Player (18, 	CMF, 	T2, 	-1, 	F, B, 60, 50, 50, 70);
    	Players(19) := new Player (19, 	CMF, 	T2, 	-1, 	F, B, 60, 40, 80, 70);
    	Players(20) := new Player (20, 	RMF, 	T2, 	-1, 	F, B, 60, 50, 50, 70);
    	Players(21) := new Player (21, 	LFW, 	T2, 	-1, 	F, B, 60, 40, 70, 70);
    	Players(22) := new Player (22, 	RFW, 	T2, 	-1, 	F, B, 60, 60, 60, 70);

    	-- inizializza le squadre in campo
    	for i in 1..11 loop
    		T1(i) := Players(i);
    		T2(i) := Players(i + 11);
    	end loop;

    	-- timer
    	Tmr := new Timer(Tm, T1, T2, M1, M2, T);

        Ada.Text_IO.Put_Line ("");
        Ada.Text_IO.Put_Line ("Server started, embedded name server is at:");
        Ada.Text_IO.Put_Line ("");
        Ada.Text_IO.Put ("export POLYORB_DSA_NAME_SERVICE=");
        Ada.Text_IO.Put_Line (PolyORB.Parameters.Get_Conf ("dsa", "name_service", ""));
        Ada.Text_IO.Put_Line ("");
        
        ---------------------------------
        -- Check for Players Positions --
        ---------------------------------
        -- PP := F.GetBallPosition;
        -- Ada.Text_IO.Put_Line("Ball: Row " & Integer'Image(PP.Row) & " - Col " & Integer'Image(PP.Col));
		-- for i in 1..22 loop
		-- 	if (i < 12) then
		-- 		PP := F.GetPlayerPosition(i);
        --         Ada.Text_IO.Put_Line("Player " & Integer'Image(i) & ": Row " & Integer'Image(PP.Row) & " - Col " & Integer'Image(PP.Col));
		-- 	else
		-- 		PP := F.GetPlayerPosition(i);
        --         Ada.Text_IO.Put_Line("Player " & Integer'Image(i) & ": Row " & Integer'Image(PP.Row) & " - Col " & Integer'Image(PP.Col));
		-- 	end if;
		-- end loop;

        -- messaggio End server
        Ada.Text_IO.Put_Line("*** - Server Completed. Up and Running - ***");

        -- loop
        --     Ada.Text_IO.Put_Line("*** - Server Up and Running - ***");
        --     delay 2.5;
        -- end loop;

	end Start;

end Server;
